home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / delphi.swg / 0189_Component to display while waiting.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-11-29  |  9.0 KB  |  366 lines

  1. {
  2. After I got the new Swag-snippets and saw how to make components for
  3. Delphi, I just made this one for fun.
  4.  
  5. You all still remember Knight Rider with his car, KITT... Well, at the
  6. front the car had a scanner... I made it as a component...
  7.  
  8. All properties are as obvious as can be (I think) so that shouldn't be a
  9. problem. Just install it as normal (Don't forget to make a bitmap for
  10. it!)
  11.  
  12. Remember, it's just for fun. Use it when scanning something or waiting
  13. for something...
  14.  
  15. Author: Martijn Tonies
  16. Date    : 10-28-1996
  17.  
  18. E-mail: M.Tonies@hsbos.nl
  19.  
  20. {---8<------------------------------------------------------------------------}
  21.  
  22. unit UKITScan;
  23.  
  24. interface
  25.  
  26. uses
  27.     SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  28.     Forms, Dialogs, ExtCtrls;
  29.  
  30. const
  31.     MaxLeds = 100;
  32.  
  33. type
  34.     TScanColor = (scBlue,scGreen,scRed,scYellow);
  35.     TScanMode  = (smLeftToRight,smRightToLeft,smBoth);
  36.  
  37.     TKITScanner = class(TGraphicControl)
  38.     private
  39.         FBevelInner:    TPanelBevel;
  40.         FBevelOuter:    TPanelBevel;
  41.         FBevelWidth:    Byte;
  42.         FHowManyLeds: Byte;
  43.         FOutColor:      TColor;
  44.         FOnColor:       TColor;
  45.         FScanColor:     TScanColor;
  46.         FScanSpeed:     Integer;
  47.         FScanning:      Boolean;
  48.         FScanMode:      TScanMode;
  49.         FLedsColors:    array [1..MaxLeds] of TScanColor;
  50.  
  51.         LedPos:             Byte;
  52.         LedWay:             Boolean;
  53.         Border:             Byte;
  54.         LedH,LedW:      Integer;
  55.         LedX:               array [1..MaxLeds] of Integer;
  56.         ScanTimer:      TTimer;
  57.         procedure DoScan(Sender: TObject);
  58.         procedure Draw;
  59.         procedure DrawBevel(Rect: TRect);
  60.         procedure DrawLeds;
  61.         procedure SetBevelInner(Value: TPanelBevel);
  62.         procedure SetBevelOuter(Value: TPanelBevel);
  63.         procedure SetBevelWidth(Value: Byte);
  64.         procedure SetHowManyLeds(Value: Byte);
  65.         procedure SetScanColor(Value: TScanColor);
  66.         procedure SetScanMode(Value: TScanMode);
  67.         procedure SetScanning(Value: Boolean);
  68.         procedure SetScanSpeed(Value: Integer);
  69.         procedure UpdateBorder;
  70.         procedure UpdatePos;
  71.         { Private declarations }
  72.     protected
  73.  
  74.         { Protected declarations }
  75.     public
  76.         constructor Create(AOwner: TComponent); override;
  77.         destructor Destroy; override;
  78.         procedure Paint; override;
  79.         { Public declarations }
  80.     published
  81.         property Align;
  82.         property BevelInner: TPanelBevel read FBevelInner write SetBevelInner default bvNone;
  83.         property BevelOuter: TPanelBevel read FBevelOuter write SetBevelOuter default bvRaised;
  84.         property BevelWidth: Byte read FBevelWidth write SetBevelWidth default 1;
  85.         property Color;
  86.         property Cursor;
  87.         property Enabled;
  88.         property HowManyLeds: Byte read FHowManyLeds write SetHowManyLeds default 7;
  89.         property ScanColor: TScanColor read FScanColor write SetScanColor default scRed;
  90.         property ScanMode: TScanMode read FScanMode write SetScanMode default smBoth;
  91.         property Scanning: Boolean read FScanning write SetScanning default False;
  92.         property ScanSpeed: Integer read FScanSpeed write SetScanSpeed default 100;
  93.         property ShowHint;
  94.         property Visible;
  95.         { Published declarations }
  96.     end;
  97.  
  98. procedure Register;
  99.  
  100. implementation
  101. {==============================================================================}
  102. {Private functions and procedures}
  103. procedure TKITScanner.Draw;
  104. var R: TRect;
  105. begin
  106.     R:=GetClientRect;
  107.     UpdateBorder;
  108.     Drawbevel(R);
  109.  
  110.     InflateRect(R,-Border,-Border);
  111.     Canvas.Brush.Style:=bsSolid;
  112.     Canvas.Brush.Color:=Color;
  113.     Canvas.FillRect(R);
  114.  
  115.     DrawLeds;
  116. end;
  117. {------------------}
  118. procedure TKITScanner.DrawBevel(Rect: TRect);
  119. var
  120.     TopColor: TColor;
  121.     BottomColor: TColor;
  122.  
  123.     procedure SetColors(Bevel: TPanelBevel);
  124.     begin
  125.         if Bevel=bvLowered
  126.         then TopColor:=clBtnShadow
  127.         else TopColor:=clBtnHighlight;
  128.         if Bevel=bvLowered
  129.         then BottomColor:=clBtnHighlight
  130.         else BottomColor:=clBtnShadow;
  131.     end;
  132.  
  133. begin
  134.     if FBevelOuter<>bvNone
  135.     then begin
  136.                  SetColors(FBevelOuter);
  137.                  Frame3D(Canvas,Rect,TopColor,BottomColor,FBevelWidth);
  138.              end;
  139.     if FBevelInner<>bvNone
  140.     then begin
  141.                  SetColors(FBevelInner);
  142.                  Frame3D(Canvas,Rect,TopColor,BottomColor,FBevelWidth);
  143.              end;
  144. end;
  145. {------------------}
  146. procedure TKITScanner.SetBevelInner(Value: TPanelBevel);
  147. begin
  148.     if Value<>FBevelInner
  149.     then begin
  150.                  FBevelInner:=Value;
  151.                  Draw;
  152.              end;
  153. end;
  154. procedure TKITScanner.SetBevelOuter(Value: TPanelBevel);
  155. begin
  156.     if Value<>FBevelOuter
  157.     then begin
  158.                  FBevelOuter:=Value;
  159.                  Draw;
  160.              end;
  161. end;
  162. procedure TKITScanner.SetBevelWidth(Value: Byte);
  163. begin
  164.     if Value<>FBevelWidth
  165.     then begin
  166.                  FBevelWidth:=Value;
  167.                  Draw;
  168.              end;
  169. end;
  170. procedure TKITScanner.UpdateBorder;
  171. begin
  172.     Border:=0;
  173.     if FBevelInner<>bvNone
  174.     then Border:=FBevelWidth;
  175.     if FBevelOuter<>bvNone
  176.     then Inc(Border,FBevelWidth);
  177. end;
  178. {------------------}
  179. procedure TKITScanner.SetHowManyLeds(Value: Byte);
  180. begin
  181.     if Value=0
  182.     then Value:=1;
  183.     if Value>MaxLeds
  184.     then Value:=MaxLeds;
  185.     if FHowManyLeds<>Value
  186.     then begin
  187.                  FHowManyLeds:=Value;
  188.                  Draw;
  189.              end;
  190. end;
  191. {------------------}
  192. procedure TKITScanner.SetScanMode(Value: TScanMode);
  193. begin
  194.     if Value<>FScanMode
  195.     then FScanMode:=Value;
  196. end;
  197. {------------------}
  198. procedure TKITScanner.SetScanSpeed(Value: Integer);
  199. begin
  200.     if Value<>FScanSpeed
  201.     then FScanSpeed:=Value;
  202.     if FScanning and Assigned(ScanTimer)
  203.     then ScanTimer.Interval:=FScanSpeed;
  204. end;
  205. {------------------}
  206. procedure TKITScanner.SetScanColor(Value: TScanColor);
  207. begin
  208.     if Value<>FScanColor
  209.     then begin
  210.                  FScanColor:=Value;
  211.                  Draw;
  212.              end;
  213. end;
  214. {------------------}
  215. procedure TKITScanner.SetScanning(Value: Boolean);
  216. begin
  217.     if Value<>FScanning
  218.     then begin
  219.                  FScanning:=Value;
  220.                  if FScanning
  221.                  then begin
  222.                                 ScanTimer:=TTimer.Create(Self);
  223.                                 ScanTimer.Interval:=FScanSpeed;
  224.                                 ScanTimer.OnTimer:=DoScan;
  225.                                 ScanTimer.Enabled:=True;
  226.                             end
  227.                  else if Assigned(ScanTimer)
  228.                             then begin
  229.                                          ScanTimer.Free;
  230.                                          ScanTimer:=nil;
  231.                                      end;
  232.              end;
  233. end;
  234. {------------------}
  235. procedure TKITScanner.DrawLeds;
  236. var n:Integer;
  237. begin
  238.     LedH:=Height-Border-Border-2;
  239.     if LedH<1
  240.     then begin
  241.                  Height:=3+Border+Border;
  242.                  Draw;
  243.              end;
  244.     LedW:=(Width-Border-Border-1-FHowManyLeds) div FHowManyLeds;
  245.     if LedW<1
  246.     then begin
  247.                  Width:=Border+Border+1+FHowManyleds*(2);
  248.                  Draw;
  249.              end;
  250.     if (Width<>(Border+Border+1+FHowManyLeds*(1+LedW))) and
  251.     ((Align=alLeft) or (Align=alRight) or (Align=alNone))
  252.     then begin
  253.                  Width:=Border+Border+1+FHowManyLeds*(1+LedW);
  254.                  Draw;
  255.              end;
  256.     case FScanColor of
  257.         scBlue      : begin
  258.                                     FOutColor:=clNavy;
  259.                                     FOnColor:=clBlue;
  260.                                 end;
  261.         scGreen     : begin
  262.                                     FOnColor:=clLime;
  263.                                     FOutColor:=clGreen;
  264.                                 end;
  265.         scRed       : begin
  266.                                     FOutColor:=clMaroon;
  267.                                     FOnColor:=clRed;
  268.                                 end;
  269.         scYellow    : begin
  270.                                     FOutColor:=clOlive;
  271.                                     FOnColor:=clYellow;
  272.                                 end;
  273.     end;
  274.  
  275.     Canvas.Brush.Color:=FOutColor;
  276.     Ledx[1]:=Border+1;
  277.     n:=2;
  278.     while n<=FHowManyLeds
  279.     do begin
  280.              Ledx[n]:=Ledx[n-1]+1+LedW;
  281.              Inc(n);
  282.          end;
  283.     for n:=1 to FHowManyLeds
  284.     do Canvas.FillRect(Rect(Ledx[n],Border+1,Ledx[n]+LedW,Height-Border-1));
  285. end;
  286. {------------------}
  287. procedure TKITScanner.UpdatePos;
  288. begin
  289.     case FScanMode of
  290.         smBoth              : if LedWay
  291.                                         then if LedPos>FHowManyLeds
  292.                                                  then LedWay:=not LedWay
  293.                                                  else Inc(LedPos,1)
  294.                                         else if LedPos<1
  295.                                                  then LedWay:=not LedWay
  296.                                                  else Dec(LedPos,1);
  297.         smLeftToRight : begin
  298.                                             LedWay:=True;
  299.                                             if LedPos>FHowManyLeds
  300.                                             then LedPos:=0
  301.                                             else Inc(LedPos,1);
  302.                                         end;
  303.         smRightToLeft : begin
  304.                                             LedWay:=False;
  305.                                             if LedPos<1
  306.                                             then LedPos:=FHowManyLeds+1
  307.                                             else Dec(LedPos,1);
  308.                                         end;
  309.     end;
  310. end;
  311.  
  312. procedure TKITScanner.DoScan;
  313. var n: Byte;
  314. begin
  315.     Canvas.Brush.Color:=FOutColor;
  316.     for n:=1 to FHowManyLeds
  317.     do Canvas.FillRect(Rect(Ledx[n],Border+1,Ledx[n]+LedW,Height-Border-1));
  318.     UpdatePos;
  319.     Canvas.Brush.Color:=FOnColor;
  320.     if (LedPos>=1) and (LedPos<=FHowManyLeds)
  321.     then Canvas.FillRect(Rect(Ledx[LedPos],Border+1,Ledx[LedPos]+LedW,Height-Border-1));
  322. end;
  323. {==============================================================================}
  324. {Protected functions and procedures}
  325.  
  326.  
  327. {==============================================================================}
  328. {Public functions and procedures}
  329. constructor TKITScanner.Create(AOwner: TComponent);
  330. begin
  331.     inherited Create(AOwner);
  332.  
  333.     FBevelInner:=bvNone;
  334.     FBevelOuter:=bvRaised;
  335.     FBevelWidth:=1;
  336.     FHowManyLeds:=7;
  337.     FScanColor:=scRed;
  338.     FScanSpeed:=100;
  339.     FScanMode:=smBoth;
  340.  
  341.     LedPos:=1;
  342.     LedWay:=True;
  343.  
  344.     Width:=82;
  345.     Height:=12;
  346. end;
  347.  
  348. destructor TKITScanner.Destroy;
  349. begin
  350.     if FScanning
  351.     then SetScanning(False);
  352.     inherited Destroy;
  353. end;
  354.  
  355. procedure TKITScanner.Paint;
  356. begin
  357.     Draw;
  358. end;
  359. {==============================================================================}
  360. procedure Register;
  361. begin
  362.     RegisterComponents('Samples', [TKITScanner]);
  363. end;
  364.  
  365. end.
  366.